<?php
require_once APP . '/application/controllers/FrontAction.php';
require_once APP . '/application/models/Util.php';

/**
 * 首页
 */
class IndexController extends FrontAction
{
    
    /**
     * 首页
     *
     */
    public function indexAction()
    {
    	// 读取通知公告
    	$sql = 'select aid,title,pubtime,keywords from article where cid=' . $this->cfg['article_types']['tzgg']['id'] . ' order by seq desc,aid desc limit 5';
    	$arr_tzgg = $this->db->fetchAll($sql);
    	
    	// 学术活动
    	$sql = 'select aid,title,pubtime,keywords from article where cid=' . $this->cfg['article_types']['xshd']['id'] . ' order by seq desc,aid desc limit 5';
    	$arr_xshd = $this->db->fetchAll($sql);

    	// 项目计划
    	$sql = 'select aid,title,pubtime,keywords from article where cid=' . $this->cfg['article_types']['xmjh']['id'] . ' order by seq desc,aid desc limit 5';
    	$arr_xmjh = $this->db->fetchAll($sql);

		// 媒体报道
    	$sql = 'select aid,title,pubtime,afrom from article where cid=' . $this->cfg['article_types']['mtbd']['id'] . ' order by seq desc,aid desc limit 3';
    	$arr_mtbd = $this->db->fetchAll($sql);

    	// 图书资料
    	$sql = 'select aid,title,pubtime,afrom from article where cid=' . $this->cfg['article_types']['tuzl']['id'] . ' order by seq desc,aid desc limit 5';
    	$arr_tuzl = $this->db->fetchAll($sql);
    	
    	// 读取出版物 每个分类读取一个
    	$obj_alllist = new Alllist($this->db);
    	$arr_pub_types = $obj_alllist->getAllListByType($this->cfg['publication_list_type'], false);
    	
    	$sql = array();
    	foreach ($arr_pub_types as $typeid => $v){
    		$sql[] = "(select pid,typeid,intro,image,t1.name,t2.name as gname from publication t1
			left join alllist t2 on t2.id=t1.typeid
    		where typeid={$typeid} order by t1.seq desc,pid desc limit 1)";
    	}
    	$arr_pub_data = array();
    	if (count($sql) > 0)
    		$arr_pub_data = $this->db->fetchAll(implode(" UNION ALL ", $sql));
    	
    	foreach ($arr_pub_data as $key => $v){
    		$arr_pub_data[$key]['url'] = $this->url($this->view->baseUrl, 'publication', 'show','id=' . $v['pid']);
    		//$arr_pub_data[$key]['turl'] = $this->url($this->view->baseUrl, 'publication', 'list','typeid=' . $v['typeid']);
    		$arr_pub_data[$key]['image'] = $this->view->baseUrl . $v['image'];
    	}
    	
    	$this->view->arr_pub_data = $arr_pub_data;
    	
    	$this->view->tzgg = $arr_tzgg;
    	$this->view->xshd = $arr_xshd;
    	$this->view->xmjh = $arr_xmjh;
    	$this->view->mtbd = $arr_mtbd;
    	$this->view->tuzl = $arr_tuzl;
    	
        $this->render("index");
    }
    
    /**
     * 搜索
     */
    public function searchAction()
    {
        $query = $this->getParam("q", "");
        $total = (int)$this->getParam("total", 0);
        $p = (int)$this->getParam("p", 1);
        
        $per_page = 10;
        
        $query = str_replace(array('%', '?', '"', '\''), '', $query);
        $query = mysql_real_escape_string($query);
       // $query = "%";
        
        if (empty($query)){
                $this->redirectAction("index", "index");
        }
        
        $sql = array();
        $sql[] = '(select "article" as type, aid as id,cid,title,content from article where title like "%' . $query . '%" order by seq desc,aid desc)';
        $sql[] = '(select "publication" as type, pid as id,typeid as cid, name as title,intro as content from publication where name like "%' . $query . '%" order by seq desc,pid desc)';
        
        if (empty($total)){
        	$countsql = 'select count(*) from (' . implode(" union ", $sql) . ') ttt';
        	$total = $this->db->fetchOne($countsql);
        }
        
        $pageInfo = Util::page ($total, $per_page, $p);
        $start    = $pageInfo ['start'];
        
        $searchsql = 'select * from (' . implode(" union ", $sql) . ') tt2 order by id desc limit ' . $start . ',' . $per_page;
        $list = $this->db->fetchAll($searchsql);
         
        // 分类处理
        $arr_publication = $this->view->types['publication'];
        
        $arr_articles = array();
        foreach ($this->view->types['zxgk'] as $v){
        	$v['c'] = 'zxgk';
        	$arr_articles[$v['id']] = $v;
        }
        foreach ($this->view->types['zzjg'] as $v){
        	$v['c'] = 'zzjg';
        	$arr_articles[$v['id']] = $v;
        }
        foreach ($this->view->types['fwxz'] as $v){
        	$v['c'] = 'fwxz';
        	$arr_articles[$v['id']] = $v;
        }
        foreach($this->cfg['article_types'] as $v){
        	$v['c'] = 'article';
        	$arr_articles[$v['id']] = $v;
        }
        
        foreach ($list as $key => $v){
        	$content = strip_tags($v['content']);
        	if (mb_strlen($content) > 330)
        		$content = mb_strcut($content, 0, 330, "utf-8") . ' ……';
        
        	$list[$key]['content'] = $content;
        	
        	if ($v['type'] == 'publication'){
	        	$list[$key]['turl'] = $this->url($this->view->baseUrl, "publication", "list", "id=" . $v['cid']);
	        	$list[$key]['tname'] =$arr_publication[$v['cid']]['name'];
	        	$list[$key]['url'] = $this->url($this->view->baseUrl, "publication", "show", "id=" . $v['id']);
        	}else{
        		$list[$key]['turl'] = $this->url($this->view->baseUrl, $arr_articles[$v['cid']]['c'], "list", "id=" . $v['cid']);
        		$list[$key]['tname'] = $arr_articles[$v['cid']]['name'];
        		$list[$key]['url'] = $this->url($this->view->baseUrl, $arr_articles[$v['cid']]['c'], "show", "id=" . $v['id']);
        	}
        }
        
        $this->view->list = $list;
        $this->view->total = $total;
        $this->view->page = $pageInfo['page'];
        $this->view->pagenum = Util::pageNumStyle($pageInfo['page']);
        $this->view->q = $query;
        
        $this->render("search");
    }
}
